package com.yesep.learn.design.behavioral.interpreter.calculator;

import java.util.function.*;

public enum OperatorType {
    ADDITION("+", 1, Integer::sum),
    SUBTRACTION("-", 1, (a, b) -> a - b),
    MULTIPLICATION("*", 2, (a, b) -> a * b),
    DIVISION("/", 2, (a, b) -> a / b);

    private final String symbol;
    private final int precedence;
    private final BiFunction<Integer, Integer, Integer> operation;

    OperatorType(String symbol, int precedence, BiFunction<Integer, Integer, Integer> operation) {
        this.symbol = symbol;
        this.precedence = precedence;
        this.operation = operation;
    }

    public static OperatorType fromSymbol(String symbol) {
        for (OperatorType operator : values()) {
            if (operator.getSymbol().equals(symbol)) {
                return operator;
            }
        }
        throw new IllegalArgumentException("Unsupported operator: " + symbol);
    }

    public static boolean isOperator(String token) {
        return "+-*/".contains(token);
    }

    public String getSymbol() {
        return symbol;
    }

    public int getPrecedence() {
        return precedence;
    }

    public int apply(int left, int right) {
        return operation.apply(left, right);
    }
}
